﻿using habbo.Habbo.Rooms;
using habbo.Kernel.Commons;
using habbo.Kernel.Storage.Querys;
using HabboEnvironment_R3.Kernel.Pathfinding;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;

namespace habbo.Habbo.Rooms
{
    class RoomManager
    {

        /// <summary>
        /// Cache to buffer some sql data in.
        /// </summary>
        public Dictionary<int, Room> WeakSQLCache { get; private set; }

        /// <summary>
        /// Calculator for walking etc.
        /// </summary>
        public BlockCalculator BlockCalculator { get; private set; }

        /// <summary>
        /// Handler of alive things.
        /// </summary>
        public Timer RoomProgressTimer { get; private set; }

        public RoomManager()
        {
            WeakSQLCache = new Dictionary<int, Room>();
            BlockCalculator = new BlockCalculator(true);
        }

        public Room GetRoom(int RoomId)
        {
            Room Room;

            if (!WeakSQLCache.TryGetValue(RoomId, out Room))
            {
                Room = new Room(SystemApp.MySQLManager.GetObject(new RoomInfoQuery(RoomId)).GetOutput<DataRow>());
                WeakSQLCache.Add(RoomId, Room);
            }

            return Room;
        }

        public bool GetRoom(int RoomId, out Room Room)
        {
            Room = GetRoom(RoomId);

            return Room != null;
        }


        public ICollection<Room> GetRooms(int CharacterId)
        {
            ICollection<Room> Output = new List<Room>();

            foreach(DataRow Row in SystemApp.MySQLManager.GetObject(new RoomsQuery(CharacterId)).GetOutput<DataTable>().Rows)
            {
                using (RowAdapter Adapter = new RowAdapter(Row))
                {
                    int RoomId = Adapter.PopInt32("id");
                    Room Room = GetRoom(RoomId);

                    Output.Add(Room);
                }
            }
            return Output;
        }
    }

    enum NavigatorTab
    {
        PRIVATE_ITEMS = 1, RATED_ITEMS = 2, MY_RAND_FRIENDS = 3, MY_ALIVE_FRIENDS = 4,
        MY_ITEMS = 5, MY_FAV_ITEMS = 6, MY_VISITED_ITEMS = 7, SEARCHED_ITEMS = 8
    }
}
